package com.hustzp.com.xichuangzhu.poetry;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.XCZBaseFragmentActivity;
import com.hustzp.com.xichuangzhu.dao.LibraryDao;
import com.hustzp.com.xichuangzhu.model.Authors;
import com.hustzp.com.xichuangzhu.poetry.adapter.PoetryAuthorAdapter;
import com.hustzp.com.xichuangzhu.poetry.model.Works;

import java.util.ArrayList;
import java.util.List;

import se.emilsjolander.stickylistheaders.StickyListHeadersListView;

/**
 * Copyright (C) 2015 The huimai mobile client Project
 * All right reserved.
 *
 * @author: wuzhimin@huimai365.com
 * @date: 2016/5/31 16:06
 * @Description:
 */
public class PoetryAuthorAct extends XCZBaseFragmentActivity implements AdapterView.OnItemClickListener {
    private StickyListHeadersListView stickyList;
    private PoetryAuthorAdapter mAdapter;
    private ImageButton img_menu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_author);
        img_menu = (ImageButton) findViewById(R.id.img_menu);
        stickyList = (StickyListHeadersListView) findViewById(R.id.list);
        stickyList.setOnItemClickListener(this);
        View headerView = getLayoutInflater().inflate(R.layout.activity_author_header, null);
        Works works = (Works) getIntent().getSerializableExtra(Works.class.getSimpleName());
        LibraryDao dao = new LibraryDao(this);
        Authors authors = dao.getAuthors(works);
        List<Works> worksList = dao.getWorksOrderby(works);
        showData(authors, worksList, headerView);
        stickyList.addHeaderView(headerView);
        mAdapter = new PoetryAuthorAdapter(this, worksList);
        stickyList.setDrawingListUnderStickyHeader(true);
        stickyList.setAreHeadersSticky(true);
        stickyList.setAdapter(mAdapter);
    }

    private void showData(Authors authors, List<Works> works, View headerView) {
        TextView tv_author = (TextView) headerView.findViewById(R.id.tv_author);
        TextView tv_dynasty = (TextView) headerView.findViewById(R.id.tv_dynasty);
        TextView tv_intro = (TextView) headerView.findViewById(R.id.tv_intro);
        TextView tv_works_num = (TextView) headerView.findViewById(R.id.tv_works_num);

        tv_author.setText(authors.getName());
        tv_dynasty.setText("[" + authors.getDynasty() + "] " + authors.getBirth_year() + "~" + authors.getDeath_year());
        tv_intro.setText(authors.getIntro());
        tv_works_num.setText("作品 / " + works.size());
        img_menu.setTag(authors);
    }

    public void openBaike(View v) {
//        Works works = (Works) v.getTag();
//        Uri uri = Uri.parse(works.getBaidu_wiki());
//        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
//        startActivity(intent);
    }

    public void openMenu(View v) {
        final Authors authors = (Authors) v.getTag();
        List<String> menus = new ArrayList<>();
        menus.add("百科");

        initSharePopupWindow();
        showSharePopupWindow(menus, new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i) {
                    case 0:
                        hideAlert();
                        openBaike(authors.getBaidu_wiki());
                        break;
                }
            }
        });
    }

    public void onClose(View v) {
        finish();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Works works = (Works) parent.getAdapter().getItem(position);
        Intent intent = new Intent(this, PoetryDetailAct.class);
        intent.putExtra("from", PoetryAuthorAct.class.getSimpleName());
        intent.putExtra(Works.class.getSimpleName(), works);
        startActivity(intent);
    }
}
